package ar.com.fiuba.application.api;

import ar.com.fiuba.domain.Word;
import ar.com.fiuba.domain.api.WordFactory;
import ar.com.fiuba.domain.exceptions.DomainException;

/**
 * Class for database loading. It uses a WordFactory to create words and a
 * WordDao to persist them.
 * 
 */
public class DataBaseLoader {

	private WordFactory wordFactory;

	private GenericDao<Word> wordDao;

	public DataBaseLoader(WordFactory wordFactory, GenericDao<Word> wordDao) {
		this.wordFactory = wordFactory;
		this.wordDao = wordDao;
	}

	/**
	 * Loads data in the database the DAO manages.
	 * 
	 * @param dataSource
	 *            source of data.
	 */
	public void loadData(DataSource dataSource) {
		while (dataSource.hasDataLeft()) {
			String dataItem = dataSource.getNextWord();
			Word word = null;
			try {
				word = wordFactory.makeWord(dataItem);
			} catch (DomainException e) {
				// TODO log
			}
			if (word != null)
				wordDao.save(word);
		}
	}

}
